<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>{{ id ? '编辑' : '添加' }}月卡</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="ruleForm" label-width="100px" :model="addForm" :rules="rules">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="addForm.personName" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="addForm.phoneNumber" />
            </el-form-item>
            <el-form-item label="车牌号码" prop="carNumber">
              <el-input v-model="addForm.carNumber" />
            </el-form-item>
            <el-form-item label="车牌品牌" prop="carBrand">
              <el-input v-model="addForm.carBrand" />
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <!--  -->
    <main class="add-main">
      <div class="form-container">
        <div class="title">最近一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="ruleForm" label-width="100px" :model="addForm" :rules="rules">
            <el-form-item label="有效日期" prop="rentTime">
              <el-date-picker
                v-model="addForm.rentTime"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="addForm.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="addForm.paymentMethod">
                <el-option
                  v-for="item in industyList"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button>重置</el-button>
        <el-button type="primary" @click="confirmSubmit">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { editmonthAPI, createmonthAPI, redactAPI } from '@/api/month'
export default {
  name: 'AddEnterprise',
  data() {
    return {
      addForm: {
        carBrand: '', // 车辆品牌
        carInfoId: null, // 车辆信息id
        carNumber: '', // 车牌号
        cardEndDate: '', // 最近一次月卡结束时间
        cardStartDate: '', // 最近一次月卡开始时间
        cardStatus: null, // 有效状态 0有效 1已过期
        paymentAmount: null, // 支付金额
        paymentMethod: '', // 支付方式
        personName: '', // 车主姓名
        phoneNumber: '', // 联系方式
        rechargeId: null, // 充值记录id
        rentTime: []// 时间
      },
      rules: {
        personName: [{ required: true, message: '企业名称为必填', trigger: 'blur' }],
        phoneNumber: [{ required: true, message: '法人为必填', trigger: 'blur' }],
        carNumber: [{ required: true, message: '注册地址为必填', trigger: 'blur' }],
        carBrand: [{ required: true, message: '所在行业为必填', trigger: 'blur' }],
        contact: [{ required: true, message: '企业联系人为必填', trigger: 'blur' }],
        contactNumber: [{ required: true, message: '企业联系人为必填', trigger: 'blur' }],
        businessLicenseId: [{ required: true, message: '请上传营业执照', trigger: 'blur' }],
        rentTime: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ]
      },
      // 支付方式
      industyList: [
        { value: 'WeChat',
          label: '微信'
        }, {
          value: 'Cash',
          label: '线下'
        }, {
          value: 'Alipay',
          label: '支付宝'
        }
      ]
    }
  },
  // id
  computed: {
    id() {
      return this.$route.query.id
    }
  },
  mounted() {
    if (this.id) {
      this.getRentDetail()
    }
  },
  methods: {
    // 数据回填
    async getRentDetail() {
      const res = await editmonthAPI(this.id)
      const { carBrand, carInfoId, carNumber, cardEndDate, cardStartDate, paymentAmount, paymentMethod, personName, phoneNumber, rechargeId } = res.data
      this.addForm = { carBrand, carInfoId, carNumber, cardEndDate, cardStartDate, paymentAmount, paymentMethod, personName, phoneNumber, rechargeId }
      this.addForm.rentTime = [this.addForm.cardStartDate, this.addForm.cardEndDate]
      console.log(res)
    },
    // 提交
    async confirmSubmit() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          if (this.id) {
            // 修改
            await redactAPI({
              personName: this.addForm.personName,
              phoneNumber: this.addForm.phoneNumber,
              carNumber: this.addForm.carNumber,
              carBrand: this.addForm.carBrand,
              paymentAmount: this.addForm.paymentAmount,
              paymentMethod: this.addForm.paymentMethod,
              cardStartDate: this.addForm.rentTime[0],
              cardEndDate: this.addForm.rentTime[1],
              carInfoId: this.addForm.carInfoId,
              rechargeId: this.addForm.rechargeId
            })
          } else {
            await createmonthAPI({
              personName: this.addForm.personName,
              phoneNumber: this.addForm.phoneNumber,
              carNumber: this.addForm.carNumber,
              carBrand: this.addForm.carBrand,
              paymentAmount: this.addForm.paymentAmount,
              paymentMethod: this.addForm.paymentMethod,
              cardStartDate: this.addForm.rentTime[0],
              cardEndDate: this.addForm.rentTime[1]
            })
            // 提示用户
            this.$message({
              type: 'success',
              message: '提交成功'
            })
          }
          this.$router.back()
          // 刷新列表
          this.getRentDetail()
          this.$message({
            message: `${this.id ? '编辑' : '添加'}成功`,
            type: 'success'
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.add-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left:20px;
      }
      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;
        .el-form{
          display: flex;
          flex-wrap: wrap;
          .el-form-item{
            width: 50%;
          }
        }
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
